<template>
  <div>
    <el-table height=window.screen.height stripe border style="width:100%" highlight-current-row :data="tableData"
      v-loading="loading">
      　　

      　　 <el-table-column prop="image" label="会员头像" align="center" min-width="40">
        <template slot-scope="scope">
          <el-avatar shape="square" :size="large" :src="scope.row.image">
          </el-avatar>
        </template>


      </el-table-column>
      <el-table-column prop="uid" label="会员uid" align="center" min-width="40">
      </el-table-column>
      　　 <el-table-column prop="username" label="会员名" align="center" min-width="40">
      </el-table-column>

      <el-table-column prop="gender" label="会员性别" align="center" min-width="40">
      </el-table-column>
      <el-table-column prop="birthday" label="会员生日" align="center" min-width="40">
      </el-table-column>



      <el-table-column prop="email" label="会员邮箱" align="center" min-width="40">
      </el-table-column>

      <el-table-column prop="salt" label="盐值" align="center" min-width="40">
      </el-table-column>
      <el-table-column prop="password" label="会员密码" align="center" min-width="40">
      </el-table-column>



      <el-table-column prop="isAdministrator" label="是否为管理员" align="center" min-width="40">
        <template slot-scope="scope">
          {{ scope.row.isAdministrator == '0' ? "否" : scope.row.isAdministrator == '1' ? "是" : '' }}
        </template>
      </el-table-column>
      <el-table-column prop="balance" label="会户余额" align="center" min-width="40">
      </el-table-column>

      <el-table-column prop="isDelete" label="状态" align="center" min-width="60">
        <template slot-scope="scope">
          {{ scope.row.isDelete == '0' ? "启用" : scope.row.isDelete == '1' ? "封禁" : '' }}
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" min-width="120">
        <template slot-scope="scope">
          <el-button type="info" @click="banned(scope.row.uid)">封号</el-button>

          <el-button type="info" @click="unlock(scope.row.uid)">解封</el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>

</template>

<script>
export default {
  data() {
    return {
      tableData: [{
        balance: '',
        birthday: '',
        describes: '',
        email: '',
        gender: '',
        image: '',
        isAdministrator: '',
        isDelete: '',
        password: '',
        salt: '',
        uid: '',
        username: '',
        edit: false
      },
      ],
      loading: false,
    }
  },
  methods: {
    init() {
      {

        if (localStorage.users) {
          this.tableData = JSON.parse(localStorage.users)
        } else {

          this.axios({
            method: 'GET',
            url: "https://admin.xiaoheihe.work/bapi/getallusers"
          }).then(response => {
            console.log(response.data);
            this.tableData = response.data
            localStorage.users = JSON.stringify(response.data);
            setTimeout(() => {
              removeLocalStorage(users);
            }, 300000)
          })
        }

      }
    },
    banned(id) {
      console.log(id)
      this.loading = true
      this.axios({
        method: "patch",
        url: "https://admin.xiaoheihe.work/bapi/banned_user",
        data: {
          uid: id,
        }
      }).then(res => {
        console.log(res.data)


        if (res.data == 'OK') {
          let getusers = localStorage.getItem("users");
          localStorage.removeItem(getusers);

          this.axios({
            method: 'GET',
            url: "https://admin.xiaoheihe.work/bapi/getallusers"
          }).then(response => {
            console.log(response.data);
            this.tableData = response.data
            localStorage.users = JSON.stringify(response.data);
            this.loading = false

            this.init()
            this.$message({
              type: 'info',
              message: `封锁成功`
            });
          })


        }

      }).error(() => {
        this.loading = false;
        this.$message({
          type: 'error',
          message: `网络错误请重试`
        });


      })
    },
    unlock(id) {
      console.log(id)
      this.axios({
        method: "patch",
        url: "https://admin.xiaoheihe.work/bapi/unlock_user",
        data: {
          uid: id,
        }
      }).then(res => {
        console.log(res.data)


        if (res.data == 'OK') {
          let getusers = localStorage.getItem("users");
          localStorage.removeItem(getusers);

          this.axios({
            method: 'GET',
            url: "https://admin.xiaoheihe.work/bapi/getallusers"
          }).then(response => {
            console.log(response.data);
            this.tableData = response.data
            localStorage.users = JSON.stringify(response.data);
            this.loading = false

            this.init()
            this.$message({
              type: 'info',
              message: `解锁成功`
            });

          })

        }

      }).error(() => {
        Toast.fail("网络错误请重试")
        this.showLoading = false
      })
    },
  },
  created() {
    this.init();
  }
}
</script>
